package com.forum.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.forum.bean.Articles;
import com.forum.bean.Types;
import com.forum.mapper.ArticlesMapper;
import com.forum.mapper.TypesMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TypesService {
    private final TypesMapper typesMapper;
    private final ArticlesMapper articlesMapper;

    public TypesService(TypesMapper typesMapper, ArticlesMapper articlesMapper) {
        this.typesMapper = typesMapper;
        this.articlesMapper = articlesMapper;
    }

    public void addType(String typeName) {
        Types type = new Types();
        type.setName(typeName);
        typesMapper.insert(type);
    }

    public void deleteType(Integer typeId) {
        typesMapper.deleteById(typeId);
        UpdateWrapper<Articles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("type_id", typeId);
        Articles article = new Articles();
        article.setTypeId(null);
        //删除articles表中对应的类型
        articlesMapper.update(article,updateWrapper);
    }

    public void updateType(Integer typeId, String newTypeName) {
        Types type = new Types();
        type.setId(typeId);
        type.setName(newTypeName);
        typesMapper.updateById(type);
    }

    public List<Types> getAllTypes() {
        return typesMapper.selectList(null);
    }
}
